﻿using MetroApp.Web.Model;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using System;
using System.Collections.ObjectModel;
using static MetroApp.Web.ViewModels.MainWindowViewModel;

namespace MetroApp.Web.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private string _title = "Prism Application";
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        private ObservableCollection<Artist> _artists;

        public ObservableCollection<Artist> Artists
        {
            get { return _artists; }
            set
            {
                _artists = value;
                RaisePropertyChanged("Artists");
            }
        }


        private ObservableCollection<News> _newList;
        public ObservableCollection<News> NewsList
        {
            get { return _newList; }
            set
            {
                _newList = value;
                RaisePropertyChanged("NewsList");
            }
        }

        private Artist _selectedArtist;
        private readonly IEventAggregator _ea;

        public Artist SelectedArtist
        {
            get => _selectedArtist;
            set => SetProperty(ref _selectedArtist, value);
        }

        public MainWindowViewModel(IEventAggregator ea)
        {
            Artists = new ObservableCollection<Artist>()
            {
               new Artist() { Id = 1, Name = "Artist 1" },
               new Artist() { Id = 2, Name = "Artist 2" },
               new Artist() { Id = 3, Name = "Artist 3" }
            };

            ArtistsDropDownCommand = new DelegateCommand<object>(OnArtistsDropDown);
            ArtistSelectedCommand = new DelegateCommand<object>(OnArtistSelected);
            _ea = ea;

            NewsList = new ObservableCollection<News>()
            {
                new News() { Id = 1,Title="深圳台风", Content="台风登陆深圳",Author ="Lily",PublishDate=DateTime.Now },
                new News() { Id = 2,Title="深圳楼市", Content="大家关心的楼上动态",Author ="Simon",PublishDate=DateTime.Now },
                new News() { Id = 3,Title="台湾回归", Content="众望所归",Author ="Liuming",PublishDate=DateTime.Now },


            };
        }

        private void OnArtistSelected(object obj)
        {
            // 用户已选好
            if (SelectedArtist == null) return;

            System.Diagnostics.Debug.WriteLine($"选中艺术家：{SelectedArtist.Name}");
            // 这里可导航、发事件、刷新其他区域等等
            _ea.GetEvent<ArtistSelectedEvent>().Publish(SelectedArtist);
        }

        private void OnArtistsDropDown(object obj)
        {
            // 下拉打开前：可刷新/加载
            //System.Diagnostics.Debug.WriteLine("下拉被打开");
            Console.WriteLine("下拉被打开" + obj.ToString());
        }

        public DelegateCommand<object> ArtistsDropDownCommand { get; }
        public DelegateCommand<object> ArtistSelectedCommand { get; }


        public class Artist
        {
            public int Id { get; set; }
            public string Name { get; set; }

        }
    }

    // Prism 事件
    public class ArtistSelectedEvent : PubSubEvent<Artist> { }
}
